package com.itheima.homework;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/*
* 现有若干图书信息（包含名称title、作者author、定价price）
*   需要存储到set集合中，保证集合中无重复元素，并遍历查看。
*   可以认为所有信息都相同的图书为重复数据
* */
public class Test6 {
    public static void main(String[] args) {
        //创建hashset集合,储存图书信息
        Set<Books> bookSet = new HashSet<>();
        //创建四本书的对象
        Books book1 = new Books("Java基础","佚名","128");
        Books book2 = new Books("web入门","佚名","68");
        Books book3 = new Books("vue手册","佚名","48");
        Books book4 = new Books("Java基础","佚名","128");
        //依次添加
        bookSet.add(book1);
        bookSet.add(book2);
        bookSet.add(book3);
        bookSet.add(book4);
        //遍历打印
        for (Books books : bookSet) {
            System.out.println(books);
        }
    }
}

//创建book实体类,
class Books{
    private String title;
    private String author;
    private String price;

    public Books() {
    }

    public Books(String title, String author, String price) {
        this.title = title;
        this.author = author;
        this.price = price;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Books books = (Books) o;
        return Objects.equals(title, books.title) &&
                Objects.equals(author, books.author) &&
                Objects.equals(price, books.price);
    }

    @Override
    public int hashCode() {
        return Objects.hash(title, author, price);
    }

    @Override
    public String toString() {
        return "book{" +
                "title='" + title + '\'' +
                ", author='" + author + '\'' +
                ", price='" + price + '\'' +
                '}';
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

   /* @Override
    public int compareTo(Books o) {
        int result = this.getAuthor().compareTo(o.author);
        result = result == 0 ? this.price.compareTo(o.price) : result;
        result = result == 0 ? this.title.compareTo(o.title) : result;
        return result;
    }*/
}
